<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\admin\model\OrderModel;
use think\DB;

class OrderController extends Controller
{
    /**
     * 后台订单列表列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $where = [];
        //订单号搜索
        if (isset($_GET['order_num']) && !empty($_GET['order_num'])) {
            $where[] = ['order_num', 'like', '%'.$_GET['order_num'].'%'];
        }
        //显示后台订单列表
        $data = OrderModel::with('goods,user')->where($where)->all();
        $data->visible(['goods'=>['name','goods_detail'], 'user'=>['nick_name','tel']])->toArray();
        //判断
        if ($data) {
            return \sendResponse($data);
        }

        return \sendResponse([]);
    }
    
    /**
     * 前台用户订单列表列表
     *
     * @return \think\Response
     */
    public function userIndex(Request $request)
    {
        //获取用户id
        $id = $request->uuid;
        //显示后台订单列表
        $data = OrderModel::with('goods,user')->where('u_id', $id)->order('id desc')->all();
        //判断是否为空
        if (!$data) {
            return \sendResponse([]);
        }      
        $data->visible(['goods'=>['name','goods_detail'], 'user'=>['nick_name','tel']])->toArray();
        //将用户相同订单号放入一个数组中
        $returnData = [];
        foreach($data as $k => $v){
            if(isset($returnData[$v['order_num']])){
                $returnData[$v['order_num']]['product'][] = [
                    'goods' => $v['goods'],
                    'goods_num'  => $v['goods_num'],
                    'goods_price'   => $v['goods_price'],
                    'goods_specs' => $v['goods_specs'],
                    // 'name' => $v['user'],
                    // 'remarks' => $v['remarks'],
                    
                ];
                //备注信息
                $returnData[$v['order_num']]['remarks'] = $v['remarks'];
                //用户信息
                $returnData[$v['order_num']]['name'] = $v['user'];
                //统计每个订单总价
                $returnData[$v['order_num']]['goods_total_price'] = $v['goods_total_price'] + $returnData[$v['order_num']]['goods_total_price'];
            }else{
                $returnData[$v['order_num']] = [
                'order_num' => $v['order_num'],
                'product' => [[
                    'goods' => $v['goods'],
                    'goods_num'  => $v['goods_num'],
                    'goods_price'   => $v['goods_price'],
                    'goods_specs' => $v['goods_specs'] 
                ]],
                'name'  => $v['user'],
                'goods_total_price' => $v['goods_total_price'],
                'remarks'  => $v['remarks'],
                ];
            }
        }
        return \sendResponse(array_values($returnData));

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //模拟传入的二维数据
        $arr = [
            ['id'=> 3, 'goods_num'=> 2, 'goods_price'=> '23.5', 'goods_specs'=> 'xxxl', 'goods_total_price'=> '50', 'remarks'=> '11', 'goods_id'=> 2],
            ['id'=> 2, 'goods_num'=> 3, 'goods_price'=> '2.5', 'goods_specs'=> 's', 'goods_total_price'=> '10', 'remarks'=> '送袜子', 'goods_id'=> 3],
        ];
        // //接收参数
        // $arr = $request->post('orders');
        //追加用户id
        $uid = $request->uuid;
        //追加订单号
        $order_num  = '2020'.$uid.time();
        //开启事务
        DB::startTrans();
        try{
            //循环添加
            foreach ($arr as $k=>$v) {
                //追加用户id
                $v['u_id'] = $uid;
                //追加订单号
                $v['order_num'] = $order_num;
                //添加
                OrderModel::create($v);
            }
            //提交
            DB::commit();
            return \sendResponse();
        } catch (\Exception $e) {
            DB::rollback();
            return \sendResponse('', -1);
        }
        
    }

    /**
     * 前台用户提交退款信息
     *
     * 
     * @return \think\Response
     */
    public function userRefund(Request $request)
    {
        // 接收退款原因
        $reason = $request->post("reason");
        //判断退款原因是否为空
        if (empty($reason)) {
            return \sendResponse('', 5001);
        }
        //接收id
        $id = $request->post("id");
        //修改状态
        $res = OrderModel::update(['status'=> 4, 'return_reason'=> $reason], ['id'=> $id]);
        if ($res) {
            return \sendResponse();
        }

        return \sendResponse('', -1);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 后台管理修改订单状态为配送当中
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function distribution($id)
    {               
        //修改状态
        $res = OrderModel::update(['status'=> 2], ['id'=> $id]);
        if ($res) {
            return \sendResponse();
        }

        return \sendResponse('', -1);
    }

    /**
     * 前台用户确认收货
     *
     */
    public function userConfirm($id)
    {
        //修改状态
        $res = OrderModel::update(['status'=> 3], ['id'=> $id]);
        if ($res) {
            return \sendResponse();
        }

        return \sendResponse('', -1);
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
